#include "ThreadPool.h"
#include "Task.h"
#include <iostream>

using ULONG = unsigned long long;
class PrintTask : public Task {
public:
  explicit PrintTask(int id) : id_(id) {}
  Any run() override {
    std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "Task " << id_ << " executed\n";
    return {};
  }

private:
  int id_;
};

class CountSum : public Task {
public:
  CountSum(ULONG begin, ULONG end): begin_(begin), end_(end){}
  Any run() {
    ULONG sum{};
    for (ULONG i = begin_ ; i != end_ + 1; i ++) {
      sum += i;
    }

    return sum;
  };
private:
  ULONG begin_{};
  ULONG end_{};
};

int main() {
  ThreadPool pool;
  pool.start(4);

  auto res_sptr = pool.submitTask(std::make_shared<CountSum>(1, 100));
  Any anyValue = res_sptr->get();
  ULONG value = anyValue.cast_<ULONG>();
  std::cout << "计算结果：" << value << std::endl;

  // Master-Slave
  
  auto res_sptr2 = pool.submitTask(std::make_shared<CountSum>(1, 100'000'000));
  ULONG sum2 = res_sptr2->get().cast_<ULONG>();

  auto res_sptr3 = pool.submitTask(std::make_shared<CountSum>(100'000'000, 200'000'000));
  ULONG sum3 = res_sptr3->get().cast_<ULONG>();

  auto res_sptr4 = pool.submitTask(std::make_shared<CountSum>(200'000'000, 300'000'000));
  ULONG sum4 = res_sptr4->get().cast_<ULONG>();
  
  std::cout << "计算结果：" << sum2 + sum3 + sum4 << std::endl;


  return 0;
}